package com.lcz.demo.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.time.ZonedDateTime;
//告诉spring可以使得存放java类异常的方法
@ControllerAdvice
public class ApiExceptionHandle {
    private final Logger logger= LoggerFactory.getLogger(ApiExceptionHandle.class);
    //处理异常类的方法
    @ExceptionHandler(value = ApiRequestException.class)
    public ResponseEntity<Object> handleApiException(ApiRequestException e){
        //根据apiRequestExcepion类生成apiException类
        ApiException apiException =new ApiException(
                e.getMessage(),
                e,
                HttpStatus.BAD_REQUEST,
                ZonedDateTime.now()
        );
        //将apiException和http的状态返回给客户端
        return new ResponseEntity<>(apiException,HttpStatus.BAD_REQUEST);
    }

    @ExceptionHandler(value = NotFoundException.class)
    public ResponseEntity<Object> handleNotFoundException(NotFoundException e){
        //根据apiRequestExcepion类生成apiException类
        ApiException apiException =new ApiException(
                e.getMessage(),
                e,
                HttpStatus.NOT_FOUND,
                ZonedDateTime.now()
        );
        //将apiException和http的状态返回给客户端
        return new ResponseEntity<>(apiException,HttpStatus.NOT_FOUND);
    }

}
